package com.elvin.platform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.elvin.platform.beans.*;
import com.elvin.platform.controller.SendWxTemplateMsg;
import com.elvin.platform.mapper.AlarmRuleMapper;
import com.elvin.platform.mapper.DeviceMapper;
import com.elvin.platform.mapper.HistoryDataMapper;
import com.elvin.platform.service.DeviceService;
import com.elvin.platform.service.HistoryDataService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * <p>
 * 历史数据表 服务实现类
 * </p>
 *
 * @author zhenchl
 * @since 2022-05-12
 */
@Service
@Log4j2
public class HistoryDataServiceImpl extends ServiceImpl<HistoryDataMapper, HistoryData> implements HistoryDataService {

    @Resource
    private  HistoryDataMapper mapper;
    @Resource
    private DeviceService deviceService;
    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private AlarmRuleMapper alarmRuleMapper;
    @Override
    public int selectPageCount(HistoryData entity) {
        return mapper.selectPageCount(entity);
    }

    @Override
    public List<HistoryData> selectForPage(HistoryData entity) {
        return mapper.selectForPage(entity);
    }

    @Override
    public void sendWxMsg(Map<String,Object> map)throws Exception {
        //1、根据devId获取信息
        log.info("发送微信消息组装数据开始");
        List<Device> deviceList = deviceMapper.selectByDevId(map);
        log.info("根据devId获取设备信息:"+deviceList.get(0).toString());
        String keyword4Value = deviceList.get(0).getLocation();
        JSONObject obj = JSONObject.parseObject(keyword4Value);
        keyword4Value = (String)obj.get("address");
        String keyword1Value = (String) map.get("keyword1Value");
        String keyword2Value = deviceList.get(0).getImei();
        String keyword3Value = (String) map.get("keyword3Value");

        //对模板数据进行组装
        Miniprogram miniprogram = new Miniprogram();
        miniprogram.setAppid(ConstantsPath.MINIAPI);//小程序ID
        miniprogram.setPagepath("pages/index/index");

        TemplateMsg templateMsg = new TemplateMsg();
        templateMsg.setAppid(ConstantsPath.APPID);//公众号ID
        templateMsg.setTemplate_id(ConstantsPath.TEMPLATE_ID);
        templateMsg.setUrl("http://weixin.qq.com/download");
        TreeMap<String, TreeMap<String, String>> tmap = new TreeMap<String, TreeMap<String, String>>();
        tmap.put("first" ,templateMsg.item("您的设备有新的报警", "#FF5757"));
        tmap.put("keyword1" ,templateMsg.item(keyword1Value, "#FF5757"));
        tmap.put("keyword2" ,templateMsg.item(keyword2Value, "#FF5757"));
        tmap.put("keyword3" ,templateMsg.item(keyword3Value, "#FF5757"));
        tmap.put("keyword4" ,templateMsg.item(keyword4Value, "#FF5757"));
        tmap.put("remark" ,templateMsg.item("请关注并及时处理您的设备报警", "#FF5757"));
        templateMsg.setData(tmap);
        //2、根据deviceId获取openId
        WxTemplateMsg msg = new WxTemplateMsg();
        msg.setMp_template_msg(templateMsg);
        SendWxTemplateMsg send = new SendWxTemplateMsg();
        //根据设备id获取alarm_rule表中配置的openid
        List<AlarmRule> alarmRuleList = alarmRuleMapper.getAlarmRuleByParam(map);
        for (AlarmRule alarmRule : alarmRuleList) {
            log.info("循环发送数据");
            msg.setTouser(alarmRule.getOpenid());//openId
            String data = obj.toJSONString(msg);
            send.sendTemplateMsg(data);
        }
    }
}
